package com.lgy.service.impl;

import com.lgy.beans.ProductInfo;
import com.lgy.dto.CartDTO;
import com.lgy.enums.ProductStatusEnum;
import com.lgy.enums.ResultEnum;
import com.lgy.exception.SellException;
import com.lgy.repository.ProductInfoRepository;
import com.lgy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRepository repository;

    @Override
//    @Cacheable(cacheNames = "product", key = "#productId")
    public ProductInfo findOne(String productId) throws SellException {
        ProductInfo productInfo = null;
        try {
            productInfo = repository.findById(productId).get();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        return productInfo;
    }

    @Override
    public List<ProductInfo> findUpAll() throws SellException {
        List<ProductInfo> productInfoList = null;
        try {
            productInfoList = repository.findByProductStatus(ProductStatusEnum.UP.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        return productInfoList;
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) throws SellException {
        Page<ProductInfo> productInfoPage;
        try {
            productInfoPage = repository.findAll(pageable);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        return productInfoPage;
    }

    @Override
//    @CachePut(cacheNames = "product", key = "#productInfo.productId")
    public ProductInfo save(ProductInfo productInfo) throws SellException {
        ProductInfo result = null;
        try {
            result = repository.save(productInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        return result;
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> carDTOList) throws SellException {
        for (CartDTO cartDTO : carDTOList) {
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            int result = productInfo.getProductStock() + cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> carDTOList) throws SellException {
        for (CartDTO cartDTO : carDTOList) {
            ProductInfo productInfo = repository.findById(cartDTO.getProductId()).get();
            if(productInfo == null) {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            int result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if(result < 0) {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo onSale(String productId) throws SellException {
        ProductInfo productInfo = findOne(productId);
        if(productInfo.getProductStatusEnum() == ProductStatusEnum.UP) {
            throw new SellException("商品状态不正确");
        }
        // 更新
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) throws SellException {
        ProductInfo productInfo = findOne(productId);
        if(productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN) {
            throw new SellException("商品状态不正确");
        }
        // 更新
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());
        return repository.save(productInfo);
    }
}
